<?php

namespace RedMVC;
 
/**
 * SessionMsg class
 *  
 * @category    RedMVC
 * @author      Jan Fischer, bitWorking <info@bitworking.de>
 */
class SessionMsg{
    
    public static function addMessage($msg){
        if(!isset(Session::getInstance('SessionMsg')->msg)){
            Session::getInstance('SessionMsg')->msg = array();
        }
        $msgs   = Session::getInstance('SessionMsg')->msg;
        $msgs[] = $msg;
        Session::getInstance('SessionMsg')->msg = $msgs;
    }
    
    public static function getMessages(Layout $layout = null){        
        if(isset(Session::getInstance('SessionMsg')->msg)){
            $messages = Session::getInstance('SessionMsg')->msg;
            unset(Session::getInstance('SessionMsg')->msg);
        }
        else{
            $messages = array();
        }
        if(null === $layout){
            return $messages;
        }    
        $layout->mergeMessages($messages);
        return $messages;
    }
    
    public static function addError($error){        
        if(!isset(Session::getInstance('SessionMsg')->error)){
            Session::getInstance('SessionMsg')->error = array();
        }
        $errors = Session::getInstance('SessionMsg')->error;
        $errors[] = $error;
        Session::getInstance('SessionMsg')->error = $errors;
    }
    
    public static function getErrors(Layout $layout = null){        
        if(isset(Session::getInstance('SessionMsg')->error)){
            $errors = Session::getInstance('SessionMsg')->error;
            unset(Session::getInstance('SessionMsg')->error);
        }
        else{
            $errors = array();
        }        
        if(null === $layout){
            return $errors;
        }
        $layout->mergeErrors($errors);
        return $errors;
    }


}